﻿using System;
using System.Windows;
using System.Windows.Media;
using uTrade.Core;

namespace uTrade.Controls
{
    public class AxisText : DrawObjBase
    {
        public DataSeries Date;

        protected override int TotalItemCount { get => Date.Count; }

        public int LineCount { get; set; } = 5;

        /// <summary>
        /// 绘制X轴日期 DrawDateTxt(dc, ChartStartX, ChartStartY + ChartHeight, ChartWidth, 16, ScaleLineCount);
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="left"></param>
        /// <param name="top"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="lineCount"></param>
        protected override void DrawObj(DrawingContext dc)
        {
            double left = ChartStartX;
            double top = ChartStartY + ChartHeight;
            double width = ChartWidth;

            if (Date == null || TotalItemCount == 0)
            {
                return;
            }
            var startIndex = (int)(chartOffset / (itemWidth + itemSpace));
            var itemOffset = (int)(width / (itemWidth + itemSpace) / (LineCount + 1));

            for (var i = 0; i < LineCount + 2; i++)
            {
                if (i * itemOffset + startIndex >= TotalItemCount)
                {
                    break;
                }
                var xoffset = (int)(i * width / (LineCount + 1)) + left + 0.5 - 45;

                DateTime dt = Convert.ToDateTime(Date[i * itemOffset + startIndex]);
                var str = dt.ToString("yyyy/MM/dd");
                FormattedText txt = new FormattedText(str,
                  System.Globalization.CultureInfo.CurrentCulture,
                  FlowDirection.LeftToRight, new Typeface("Verdana"),
                  12, new SolidColorBrush(Color.FromRgb(64, 64, 64)));
                dc.DrawText(txt, new Point(xoffset, top));
            }
        }
    }
}